﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Configuration;
using System.Collections.Specialized;

namespace Twitter.Tests
{
	/// <summary>
	/// Conditional settings for all tests so that they 
	/// run against twitter or mocks depending on a project-level 
	/// directive.
	/// </summary>
	public static class TestRun
	{
		public static readonly string BotScreenName;
		public static readonly string ClientScreenName;
		public static readonly ICredentials BotCredentials;
		public static readonly ICredentials ClientCredentials;

		static TestRun()
		{
			var section = ConfigurationManager.GetSection("twitter") as NameValueCollection;
			if (section == null)
				throw new ConfigurationErrorsException("A section named 'twitter' of type NameValueSectionHandler is required.");

			BotScreenName = section["botUser"];
			ClientScreenName = section["clientUser"];
			BotCredentials = new NetworkCredential(BotScreenName, section["botPassword"]);
			ClientCredentials = new NetworkCredential(ClientScreenName, section["clientPassword"]);
		}

#if TWITTER
		public const string BaseUri = "http://twitter.com/";
		public const bool IsProduction = true;
		public const bool IsMock = false;
#else
		public const string BaseUri = "test://foo.com/";
		public const bool IsProduction = false;
		public const bool IsMock = true;
#endif

		public static Uri GetUri(string partialUri)
		{
			return new Uri(new Uri(BaseUri), partialUri);
		}

		public static IBotClient CreateBot()
		{
#if TWITTER
			return new BotClient(BotCredentials);
#else
			return new MemoryBotClient(BotScreenName);
#endif
		}

		public static IBotClient CreateClient()
		{
#if TWITTER
			return new BotClient(ClientCredentials);
#else
			return new MemoryBotClient(ClientScreenName);
#endif
		}

		public static void CreateClients(out IBotClient botClient, out IBotClient userClient)
		{
#if TWITTER
			botClient = new BotClient(BotCredentials);
			userClient = new BotClient(ClientCredentials);
#else
			botClient = new MemoryBotClient(BotScreenName);
			userClient = new MemoryBotClient(ClientScreenName);
			((MemoryBotClient)botClient).SetBuddy((MemoryBotClient)userClient);
			((MemoryBotClient)userClient).SetBuddy((MemoryBotClient)botClient);
#endif

		}
	}
}
